#!/bin/bash

set -e
set -x

source ./env.sh

iplist_path=config
while read -r line
do
    case $line in       
        *'#'*)
            continue
        ;;
        *'end'*)
            break
        ;;
        *)
            IFS=',' read -ra parts <<< "$line"
        ;;
    esac  
    
    pass="${parts[0]}" 
    address="${parts[1]}"
    hostname="${parts[2]}"
    if [ $address ];then
        sshpass -p$pass ssh -n $address sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
        sshpass -p$pass ssh -n $address setenforce 0
        sshpass -p$pass ssh -n $address systemctl disable --now firewalld
        sshpass -p$pass scp system_optimization.conf $address:/etc/sysctl.d/
        sshpass -p$pass ssh -n $address sysctl -p /etc/sysctl.d/system_optimization.conf
        sshpass -p$pass scp system_limit.conf $address:/etc/security/limits.d/
        sshpass -p$pass scp system_umask.sh $address:/etc/profile.d/
        sshpass -p$pass ssh -n $address 'cat >> /etc/ssh/sshd_config <<EOF
AllowAgentForwarding yes
AllowTcpForwarding yes
GatewayPorts yes
EOF'
        sshpass -p$pass ssh -n $address systemctl restart sshd
        sshpass -p$pass ssh -n $address nmcli con mod $netname ipv4.method manual ipv4.addresses $address/24 ipv4.gateway $gateway ipv4.dns $dns
        sshpass -p$pass ssh -n $address nmcli device connect $netname
        
    fi
done < $iplist_path

set +x
set +e
